﻿<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:Controls="clr-namespace:MahApps.Metro.Controls">

    <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />

    <Style TargetType="{x:Type ComboBox}"
           x:Key="MetroComboBox">
        <Setter Property="MinHeight"
                Value="26" />
        <Setter Property="Foreground"
                Value="{DynamicResource TextBrush}" />
        <Setter Property="Background"
                Value="{DynamicResource GrayBrush10}" />
        <Setter Property="HorizontalContentAlignment"
                Value="Left" />
        <Setter Property="VerticalContentAlignment"
                Value="Center" />
        <Setter Property="BorderThickness"
                Value="1" />
        <Setter Property="Padding"
                Value="0" />
        <Setter Property="ScrollViewer.HorizontalScrollBarVisibility"
                Value="Auto" />
        <Setter Property="ScrollViewer.VerticalScrollBarVisibility"
                Value="Auto" />
        <Setter Property="BorderBrush"
                Value="{DynamicResource TextBoxBorderBrush}" />
        <Setter Property="FontFamily"
                Value="{DynamicResource ContentFontFamily}" />
        <Setter Property="FontSize"
                Value="{DynamicResource ContentFontSize}" />
        <Setter Property="ItemContainerStyle"
                Value="{DynamicResource MetroComboBoxItem}" />
        <!-- change SnapsToDevicePixels to true to view a better border and validation error -->
        <Setter Property="SnapsToDevicePixels"
                Value="True" />
        <Setter Property="Validation.ErrorTemplate"
                Value="{DynamicResource ValidationErrorTemplate}" />
        <Setter Property="ScrollViewer.CanContentScroll"
                Value="False" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ComboBox}">
                    <Grid>
                        <Grid.Resources>
                            <Style x:Key="comboToggleStyle"
                                   TargetType="{x:Type ToggleButton}">
                                <Setter Property="Foreground"
                                        Value="{DynamicResource TextBrush}" />
                                <Setter Property="Background"
                                        Value="{DynamicResource GrayBrush10}" />
                                <Setter Property="BorderBrush"
                                        Value="{DynamicResource TextBoxBorderBrush}" />
                                <Setter Property="BorderThickness"
                                        Value="1" />
                                <Setter Property="Padding"
                                        Value="3" />
                                <Setter Property="Template">
                                    <Setter.Value>
                                        <ControlTemplate TargetType="{x:Type ToggleButton}">
                                            <Grid>
                                                <VisualStateManager.VisualStateGroups>
                                                    <VisualStateGroup x:Name="CommonStates">
                                                        <VisualState x:Name="Normal" />
                                                        <VisualState x:Name="MouseOver">
                                                            <Storyboard>
                                                                <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)"
                                                                                               Storyboard.TargetName="MouseOverBorder">
                                                                    <EasingDoubleKeyFrame KeyTime="0"
                                                                                          Value="1" />
                                                                </DoubleAnimationUsingKeyFrames>
                                                            </Storyboard>
                                                        </VisualState>
                                                        <VisualState x:Name="Pressed">
                                                            <Storyboard>
                                                                <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)"
                                                                                               Storyboard.TargetName="PressedBorder">
                                                                    <EasingDoubleKeyFrame KeyTime="0"
                                                                                          Value="1" />
                                                                </DoubleAnimationUsingKeyFrames>
                                                            </Storyboard>
                                                        </VisualState>
                                                        <VisualState x:Name="Disabled" />
                                                    </VisualStateGroup>
                                                    <VisualStateGroup x:Name="FocusStates">
                                                        <VisualState x:Name="Focused">
                                                            <Storyboard>
                                                                <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)"
                                                                                               Storyboard.TargetName="FocusRectangle">
                                                                    <EasingDoubleKeyFrame KeyTime="0"
                                                                                          Value="1" />
                                                                </DoubleAnimationUsingKeyFrames>
                                                            </Storyboard>
                                                        </VisualState>
                                                        <VisualState x:Name="Unfocused" />
                                                    </VisualStateGroup>
                                                </VisualStateManager.VisualStateGroups>

                                                <Border x:Name="Background"
                                                        BorderBrush="{TemplateBinding BorderBrush}"
                                                        BorderThickness="{TemplateBinding BorderThickness}"
                                                        Background="{TemplateBinding Background}" />
                                                <Rectangle x:Name="DisabledVisualElement"
                                                           Fill="{DynamicResource WhiteBrush}"
                                                           IsHitTestVisible="false"
                                                           Opacity="0" />
                                                <Border x:Name="MouseOverBorder"
                                                        Background="{DynamicResource GrayBrush8}"
                                                        BorderThickness="{TemplateBinding BorderThickness}"
                                                        Opacity="0" />
                                                <Border x:Name="PressedBorder"
                                                        Background="{DynamicResource GrayBrush7}"
                                                        BorderThickness="{TemplateBinding BorderThickness}"
                                                        Opacity="0" />
                                                <Rectangle x:Name="FocusRectangle"
                                                           Stroke="{DynamicResource ComboBoxMouseOverInnerBorderBrush}"
                                                           Opacity="0" />
                                                <Rectangle x:Name="FocusVisualElement"
                                                           IsHitTestVisible="false"
                                                           Margin="1"
                                                           Stroke="{DynamicResource HighlightBrush}"
                                                           StrokeThickness="1"
                                                           Visibility="Collapsed" />

                                                <Grid Margin="1">
                                                    <Grid.ColumnDefinitions>
                                                        <ColumnDefinition Width="*" />
                                                        <ColumnDefinition Width="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Grid}}, Path=ActualHeight, Mode=OneWay}" />
                                                        <ColumnDefinition Width="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Grid}}, Path=ActualHeight, Mode=OneWay}" />
                                                    </Grid.ColumnDefinitions>

                                                    <Button x:Name="PART_ClearText"
                                                            Grid.Column="1"
                                                            FontSize="16"
                                                            Style="{DynamicResource ChromelessButtonStyle}"
                                                            Foreground="{TemplateBinding Foreground}"
                                                            FontFamily="Marlett"
                                                            Content="r"
                                                            Visibility="{Binding RelativeSource={RelativeSource AncestorType={x:Type ComboBox}}, Path=(Controls:TextboxHelper.ClearTextButton), Converter={StaticResource BooleanToVisibilityConverter}}"
                                                            IsTabStop="False" />
                                                    <Rectangle x:Name="BtnArrowBackground"
                                                               Grid.Column="2"
                                                               Fill="Transparent"
                                                               StrokeThickness="0" />
                                                    <Path x:Name="BtnArrow"
                                                          Grid.Column="2"
                                                          IsHitTestVisible="false"
                                                          SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                                                          Data="F1 M 301.14,-189.041L 311.57,-189.041L 306.355,-182.942L 301.14,-189.041 Z "
                                                          HorizontalAlignment="Center"
                                                          Height="4"
                                                          Stretch="Uniform"
                                                          Width="8"
                                                          Fill="{DynamicResource GrayBrush1}" />
                                                </Grid>
                                            </Grid>
                                            <ControlTemplate.Triggers>
                                                <Trigger SourceName="BtnArrowBackground"
                                                         Property="IsMouseOver"
                                                         Value="True">
                                                    <Setter TargetName="BtnArrowBackground"
                                                            Property="Fill"
                                                            Value="{DynamicResource GrayBrush5}" />
                                                </Trigger>
                                                <Trigger SourceName="PART_ClearText"
                                                         Property="IsMouseOver"
                                                         Value="True">
                                                    <Setter TargetName="PART_ClearText"
                                                            Property="Background"
                                                            Value="{DynamicResource GrayBrush8}" />
                                                    <Setter TargetName="PART_ClearText"
                                                            Property="Foreground"
                                                            Value="{DynamicResource AccentColorBrush}" />
                                                </Trigger>
                                                <Trigger SourceName="PART_ClearText"
                                                         Property="IsPressed"
                                                         Value="True">
                                                    <Setter TargetName="PART_ClearText"
                                                            Property="Background"
                                                            Value="{DynamicResource BlackBrush}" />
                                                    <Setter TargetName="PART_ClearText"
                                                            Property="Foreground"
                                                            Value="{DynamicResource WhiteBrush}" />
                                                </Trigger>
                                            </ControlTemplate.Triggers>
                                        </ControlTemplate>
                                    </Setter.Value>
                                </Setter>
                            </Style>
                        </Grid.Resources>
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Normal" />
                                <VisualState x:Name="MouseOver" />
                                <VisualState x:Name="Disabled">
                                    <Storyboard>
                                        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)"
                                                                       Storyboard.TargetName="DisabledVisualElement">
                                            <SplineDoubleKeyFrame KeyTime="00:00:00"
                                                                  Value="0.45" />
                                        </DoubleAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                            <VisualStateGroup x:Name="FocusStates">
                                <VisualState x:Name="Focused">
                                    <Storyboard>
                                        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)"
                                                                       Storyboard.TargetName="FocusRectangle">
                                            <EasingDoubleKeyFrame KeyTime="0"
                                                                  Value="1" />
                                        </DoubleAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Unfocused" />
                                <VisualState x:Name="FocusedDropDown">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Duration="00:00:00"
                                                                       Storyboard.TargetProperty="(UIElement.Visibility)"
                                                                       Storyboard.TargetName="PopupBorder">
                                            <DiscreteObjectKeyFrame KeyTime="00:00:00">
                                                <DiscreteObjectKeyFrame.Value>
                                                    <Visibility>Visible</Visibility>
                                                </DiscreteObjectKeyFrame.Value>
                                            </DiscreteObjectKeyFrame>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Grid}}, Path=ActualHeight, Mode=OneWay}" />
                                <ColumnDefinition Width="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Grid}}, Path=ActualHeight, Mode=OneWay}" />
                            </Grid.ColumnDefinitions>

                            <ToggleButton x:Name="PART_DropDownToggle"
                                          Grid.ColumnSpan="3"
                                          BorderBrush="{TemplateBinding BorderBrush}"
                                          BorderThickness="{TemplateBinding BorderThickness}"
                                          Background="{TemplateBinding Background}"
                                          Margin="0"
                                          Style="{DynamicResource comboToggleStyle}"
                                          IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
                                          VerticalAlignment="Stretch"
                                          KeyboardNavigation.IsTabStop="False" />

                            <Border x:Name="EditableTextBoxBorder"
                                    Visibility="Collapsed"
                                    BorderBrush="Transparent"
                                    BorderThickness="1,1,0,1"
                                    Margin="0,0,-2,0"
                                    Background="{DynamicResource ControlBackgroundBrush}">
                                <TextBox x:Name="PART_EditableTextBox"
                                         HorizontalAlignment="Stretch"
                                         SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                                         HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
                                         VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                         BorderThickness="0"
                                         Background="{x:Null}"
                                         Margin="0,0,0,-2"
                                         Controls:TextboxHelper.Watermark="{TemplateBinding Controls:TextboxHelper.Watermark}"
                                         Foreground="{TemplateBinding Foreground}"
                                         Focusable="True"
                                         IsReadOnly="{TemplateBinding IsReadOnly}"
                                         FontFamily="{TemplateBinding FontFamily}"
                                         FontSize="{TemplateBinding FontSize}" />
                            </Border>
                            <Grid x:Name="ContentSite"
                                  Margin="7 0 0 0">
                                <ContentPresenter ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}"
                                                  ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"
                                                  Content="{TemplateBinding SelectionBoxItem}"
                                                  ContentStringFormat="{TemplateBinding SelectionBoxItemStringFormat}"
                                                  HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                                  IsHitTestVisible="false"
                                                  Margin="{TemplateBinding Padding}"
                                                  SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                                                  VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
                            </Grid>
                        </Grid>
                        <Rectangle x:Name="FocusRectangle"
                                   Stroke="{DynamicResource ComboBoxMouseOverInnerBorderBrush}"
                                   Opacity="0" />
                        <Rectangle x:Name="DisabledVisualElement"
                                   Fill="{DynamicResource DisabledWhiteBrush}"
                                   IsHitTestVisible="false"
                                   Opacity="0" />
                        <!-- AllowsTransparency="true" fixes the redraw problem under windows vista/7 with a selected non aero theme -->
                        <Popup x:Name="PART_Popup"
                               AllowsTransparency="true"
                               Focusable="False"
                               IsOpen="{Binding IsDropDownOpen, RelativeSource={RelativeSource TemplatedParent}}"
                               PopupAnimation="{DynamicResource {x:Static SystemParameters.ComboBoxPopupAnimationKey}}"
                               MinWidth="{Binding ActualWidth, RelativeSource={RelativeSource TemplatedParent}}"
                               MaxHeight="{Binding MaxDropDownHeight, RelativeSource={RelativeSource TemplatedParent}}"
                               Placement="Bottom">
                            <Border x:Name="PopupBorder"
                                    Effect="{DynamicResource DropShadowBrush}"
                                    BorderThickness="{TemplateBinding BorderThickness}"
                                    HorizontalAlignment="Stretch"
                                    Height="Auto"
                                    BorderBrush="{DynamicResource ComboBoxPopupBrush}"
                                    Background="{DynamicResource WhiteBrush}">
                                <ScrollViewer x:Name="DropDownScrollViewer"
                                              BorderThickness="0"
                                              Margin="2"
                                              Padding="1">
                                    <ItemsPresenter x:Name="ItemsPresenter"
                                                    KeyboardNavigation.DirectionalNavigation="Contained" />
                                </ScrollViewer>
                            </Border>
                        </Popup>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=(Controls:TextboxHelper.ClearTextButton)}"
                                     Value="False">
                            <Setter TargetName="EditableTextBoxBorder"
                                    Property="Grid.ColumnSpan"
                                    Value="2" />
                            <Setter TargetName="ContentSite"
                                    Property="Grid.ColumnSpan"
                                    Value="2" />
                        </DataTrigger>
                        <Trigger Property="IsEditable"
                                 Value="True">
                            <Setter Property="IsTabStop"
                                    Value="false" />
                            <Setter TargetName="EditableTextBoxBorder"
                                    Property="Visibility"
                                    Value="Visible" />
                            <Setter TargetName="ContentSite"
                                    Property="Visibility"
                                    Value="Hidden" />
                            <Setter TargetName="PART_DropDownToggle"
                                    Property="Focusable"
                                    Value="False" />
                            <!-- #1037 : don't know why we set this to transparent ???
                            <Setter TargetName="PART_DropDownToggle"
                                    Property="Background"
                                    Value="Transparent" /> -->
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <Style x:Key="VirtualisedMetroComboBox"
           TargetType="{x:Type ComboBox}"
           BasedOn="{StaticResource MetroComboBox}">
        <Setter Property="ItemsPanel">
            <Setter.Value>
                <ItemsPanelTemplate>
                    <VirtualizingStackPanel IsItemsHost="True"
                                            KeyboardNavigation.DirectionalNavigation="Contained"
                                            IsVirtualizing="True"
                                            VirtualizationMode="Recycling" />
                </ItemsPanelTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="ScrollViewer.CanContentScroll"
                Value="True" />
        <Style.Triggers>
            <Trigger Property="IsGrouping" Value="True">
                <Setter Property="ScrollViewer.CanContentScroll"
                        Value="False" />
            </Trigger>
        </Style.Triggers>
    </Style>

    <Style TargetType="ComboBoxItem"
           x:Key="MetroComboBoxItem">
        <Setter Property="Foreground"
                Value="{DynamicResource TextBrush}" />
        <Setter Property="Padding"
                Value="2" />
        <Setter Property="HorizontalContentAlignment"
                Value="Left" />
        <Setter Property="VerticalContentAlignment"
                Value="Top" />
        <Setter Property="Background"
                Value="{DynamicResource TransparentWhiteBrush}" />
        <Setter Property="BorderThickness"
                Value="1" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ComboBoxItem">
                    <Grid Background="{TemplateBinding Background}"
                          Margin="0,0.5">
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Normal" />
                                <VisualState x:Name="MouseOver">
                                    <Storyboard>
                                        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)"
                                                                       Storyboard.TargetName="MouseOverRectangle">
                                            <EasingDoubleKeyFrame KeyTime="0:0:0.1"
                                                                  Value=".65" />
                                        </DoubleAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Disabled">
                                    <Storyboard>
                                        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="Opacity"
                                                                       Storyboard.TargetName="contentPresenter">
                                            <SplineDoubleKeyFrame KeyTime="0"
                                                                  Value=".55" />
                                        </DoubleAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                            <VisualStateGroup x:Name="SelectionStates">
                                <VisualState x:Name="Unselected" />
                                <VisualState x:Name="Selected">
                                    <Storyboard>
                                        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)"
                                                                       Storyboard.TargetName="SelectedRectangle">
                                            <EasingDoubleKeyFrame KeyTime="0:0:0.1"
                                                                  Value="1" />
                                        </DoubleAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                            <VisualStateGroup x:Name="FocusStates">
                                <VisualState x:Name="Focused">
                                    <Storyboard />
                                </VisualState>
                                <VisualState x:Name="Unfocused" />
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <Rectangle x:Name="SelectedRectangle"
                                   IsHitTestVisible="False"
                                   Opacity="0"
                                   Fill="{DynamicResource AccentColorBrush}" />
                        <Rectangle x:Name="MouseOverRectangle"
                                   IsHitTestVisible="False"
                                   Opacity="0"
                                   Fill="{DynamicResource AccentColorBrush3}" />
                        <ContentControl Foreground="{TemplateBinding Foreground}">
                            <ContentPresenter x:Name="contentPresenter"
                                              Margin="{TemplateBinding Padding}"
                                              VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                              HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" />
                        </ContentControl>
                        <Rectangle x:Name="FocusVisualElement"
                                   Stroke="{DynamicResource HighlightBrush}"
                                   StrokeThickness="1"
                                   Visibility="Collapsed" />
                    </Grid>

                    <ControlTemplate.Triggers>
                        <Trigger Property="IsSelected"
                                 Value="True">
                            <Setter Property="Foreground"
                                    Value="{DynamicResource AccentSelectedColorBrush}" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

</ResourceDictionary>
